﻿
using Microsoft.VisualBasic.FileIO;
using System.Collections;
using System.ComponentModel.Design;
using System.Net.Mail;
using System.Reflection;


class HomeWork
{
    public static void Main(String[] args)
    {
        int c;

        Manage m = new Manage();
        Console.WriteLine("软构第四次作业:");
        Console.WriteLine("d1（输入1）,d2（输入2），d3（输入3）");
        c = Convert.ToInt32(Console.ReadLine());
        if (c == 1) { Manage.ShowDialog(); }
        else if (c == 2)
        {
            List<int> mylist2 = new List<int>();
            int sum;
            double avg;
            Random random = new Random();
            for (int i = 0; i < 100; i++)
                mylist2.Add(random.Next(0, 1000));
            sum = mylist2.Sum();
            avg = mylist2.Average();
            var l = from n in mylist2 orderby n descending select n;
            mylist2 = l.ToList();
            Console.WriteLine("生成了100个随机数......");
            Console.WriteLine("它们的和为:{0}", sum);
            Console.WriteLine("它们的平均值为:{0}", avg);
            Console.WriteLine("它们是：");
            foreach (var i in mylist2)
            {
                Console.WriteLine(i);
            }
        }
        else if (c == 3)
        {
            Type ptype = typeof(Person);
            ConstructorInfo constructor = ptype.GetConstructor(Type.EmptyTypes);
            ConstructorInfo constructor2 = ptype.GetConstructor(new Type[] { typeof(string), typeof(int) });
            if (constructor != null)
            {
                constructor.Invoke(new object[] { });
            }
            if (constructor2 != null)
            {
                constructor2.Invoke(new object[] { "小明", 18 });
            }
        }

        else
        {

        }

    }

}
class Manage
{
    public static List<Order> myList;
    public Manage()
    {
        myList = new List<Order>();
    }
    public static void ShowDialog()
    {

        while (true)
        {
            Console.WriteLine("欢迎来到订单管理系统!");
            Console.WriteLine("请选择您的操作:");
            Console.WriteLine("1:增添新订单");
            Console.WriteLine("2:删除旧订单");
            Console.WriteLine("3:查询订单");
            Console.WriteLine("4:修改订单信息");
            Console.WriteLine("5:打印当前订单列表");
            Console.WriteLine("其他:退出系统");
            int c;
            c = Convert.ToInt32(Console.ReadLine());
            if (c == 1)
            {

                Order order = orderService.Add();

                if (order != null)
                {
                    myList.Add(order);
                }
            }
            else if (c == 2) { orderService.Delete(); }
            else if (c == 3)
            {
                Console.WriteLine("请输入要查询的订单编号:");
                String? s;
                s = Console.ReadLine();
                if (orderService.FindOrder(s))
                {
                    Order? x = Manage.myList.Find(x => x.id == s);
                    Console.WriteLine("该订单信息为:");
                    Console.WriteLine("编号:{0}", x.id);
                    Console.WriteLine("商品名字:{0}", x.name);
                    Console.WriteLine("客户名字:{0}", x.client);
                    Console.WriteLine("订单金额:{0}", x.amount);
                }
                else
                {
                    Console.WriteLine("不存在订单号为{0}的订单!", s);
                }
            }
            else if (c == 4)
            {
                orderService.Alter();
            }
            else if (c == 5) { Manage.Print(); }
            else
            { }
            Console.WriteLine("按任意键返回主菜单.....");
            Console.ReadKey();

            System.Console.Clear();
        }
    }
    public static void Print()
    {
        int op;
        Console.WriteLine("请选择输出方式:1.默认,2.订单金额,3.编号");
        op = Convert.ToInt32(Console.ReadLine());
        int i = 1;
        if (op != 2 || op != 3)
        {

            foreach (var x in myList)
            {

                Console.WriteLine("第{0}号订单信息为:", i);
                Console.WriteLine("编号:{0}", x.id);
                Console.WriteLine("商品名字:{0}", x.name);
                Console.WriteLine("客户名字:{0}", x.client);
                Console.WriteLine("订单金额:{0}", x.amount);
                i++;
            }
        }
        else if (op == 2)
        {
            var l = from x in myList orderby x.amount descending select x;
            List<Order> ll = l.ToList();
            foreach (var x in l)
            {
                Console.WriteLine("第{0}号订单信息为:", i);
                Console.WriteLine("编号:{0}", x.id);
                Console.WriteLine("商品名字:{0}", x.name);
                Console.WriteLine("客户名字:{0}", x.client);
                Console.WriteLine("订单金额:{0}", x.amount);
                i++;
            }
        }
        else if (op == 3)
        {
            var l = from x in myList orderby x.id ascending select x;
            List<Order> ll = l.ToList();
            foreach (var x in l)
            {
                Console.WriteLine("第{0}号订单信息为:", i);
                Console.WriteLine("编号:{0}", x.id);
                Console.WriteLine("商品名字:{0}", x.name);
                Console.WriteLine("客户名字:{0}", x.client);
                Console.WriteLine("订单金额:{0}", x.amount);
                i++;
            }
        }
    }
}
class Order
{
    public String? id { set; get; }
    public String? name { set; get; }
    public String? client { set; get; }
    public String? amount { set; get; }

}

class orderDetail { }
class orderService
{
    public static Order Add()
    {

        Order o = new Order();
    loop2: Console.WriteLine("请输入新订单编号:");
        o.id = Console.ReadLine();
        if (orderService.FindOrder(o.id))
        {
            Console.WriteLine("订单重复，请重新输入!");
            goto loop2;
        }
        Console.WriteLine("请输入商品名称：");
        o.name = Console.ReadLine();
        Console.WriteLine("请输入客户名称：");
        o.client = Console.ReadLine();
        Console.WriteLine("请输入订单金额：");
        o.amount = Console.ReadLine();
        Console.WriteLine("添加完成！");
        return o;
    }
    public static void Delete()
    {
        Console.WriteLine("请输入要删除的订单编号:");
        String? s = Console.ReadLine();
        if (FindOrder(s))
        {
        loop1: Console.WriteLine("是否要删除该订单?:");
            Console.WriteLine("Y:是");
            Console.WriteLine("N:否");
            string? c;
            c = Console.ReadLine();
            if (c == "Y" || c == "y")
            {
                Manage.myList.RemoveAll(x => x.id == s);
            }
            else if (c == "N" || c == "n")
            {
            }
            else goto loop1;
        }
        else
        {
            Console.WriteLine("不存在订单号为{0}的订单!", s);
        }
    }
    public static void Alter()
    {
        Console.WriteLine("请输入要查询的订单编号:");
        String? s;
        s = Console.ReadLine();
        if (orderService.FindOrder(s))
        {
            Order? x = Manage.myList.Find(x => x.id == s);
            Console.WriteLine("该订单信息为:");
            Console.WriteLine("编号:{0}", x.id);
            Console.WriteLine("商品名字:{0}", x.name);
            Console.WriteLine("客户名字:{0}", x.client);
            Console.WriteLine("订单金额:{0}", x.amount);
            String g, n;
            Console.WriteLine("请输入要修改的内容:");
            Console.WriteLine("1.编号");
            Console.WriteLine("2.商品名字");
            Console.WriteLine("3.客户名字");
            Console.WriteLine("4.订单金额");
            g = Console.ReadLine();
            Console.WriteLine("请输入新数据:");
            n = Console.ReadLine();
            if (g == "1") { x.id = n; }
            else if (g == "2") { x.name = n; }
            else if (g == "3") { x.client = n; }
            else if (g == "4") { x.amount = n; }
            else { }
            Console.WriteLine("修改成功！");
        }
        else
        {
            Console.WriteLine("不存在订单号为{0}的订单!", s);
        }
    }
    public static bool FindOrder(String s)
    {
        Order? k = Manage.myList.Find(o => o.id == s);
        if (k != null) return true;
        else return false;
    }
}
class Person
{
    string name { get; set; }
    int age { get; set; }
    public Person()
    {
        Console.WriteLine("Person的无参构造！");
    }
    public Person(string name, int age) { Console.WriteLine("Person的有参构造:name:{0},age:{1}", name, age); }
}
